import 'package:app/common/tool.dart';
import 'package:app/pages/all_grid_module/all_grid_controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import '../../common/colors.dart';
import '../home_module/home_model.dart';

class AllGridView extends GetView{


  @override
  final controller = Get.put(AllGridController());

  AllGridView({super.key});


  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(

      backgroundColor: Colors.grey.shade200,
      appBar:PreferredSize(
        preferredSize: Size(Get.width, AppTool.appBarHEIGHT),
        child:AppBar(
          leading: IconButton(onPressed: () {
            Get.back(result: 'refresh');
          },icon: Image.asset('images/leftarr.png',width:15,height: 15,),),
          title: const Text('全部',style: TextStyle(color: Colors.black,fontSize: 16),),
        ),
      ),

      body: EasyRefresh.custom(

        slivers: [

         GetBuilder<AllGridController>(builder: (_){
           return  SliverList(delegate: _mySliverChildBuilderDelegate(),);
         })

        ],
      ),
    );
  }

  _mySliverChildBuilderDelegate() {
    return SliverChildBuilderDelegate(
          (BuildContext context, int index) {
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
                margin: const EdgeInsets.only(left: 15,top: 10,bottom: 10),
                color: Colors.grey.shade200,
                child:Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text('${controller.model?.data?[index].categoryName}',
                      style: TextStyle(color: AppColor.smallTextColor,
                          fontSize: 16),),
                  ],
                )
            ),

            ReorderableListView.builder(
              physics: new NeverScrollableScrollPhysics(),
                shrinkWrap: true,
                itemBuilder: (BuildContext context, int a) {
                  AppGridModel appGridModel = controller.dataArr[index];
                  Apps appModel = appGridModel.data![index].apps![a];
                  return Container(
                    color: Colors.white,
                    // padding: EdgeInsets.all(10),
                    padding: const EdgeInsets.only(left: 15,),
                    key: ValueKey(a),//必须要有唯一的 不重复的key
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Row(
                              children: [
                                Image.asset('images/drag_icon.png',width: 10,height: 10,),
                                const SizedBox(width: 5,),
                                Text('${appModel.name}',style: const TextStyle(fontSize: 15),),
                              ],
                            ),
                            SizedBox(
                              width: 50,
                              height: 40,
                              child: Switch(value: appModel.is_focus, onChanged: (value){

                                if(appModel.is_focus==true){
                                  appModel.is_focus = false;
                                  controller.requestDataWithRemove(appModel.id);
                                }else{
                                  appModel.is_focus = true;
                                  controller.requestDataWithFocus(appModel.id);
                                  // controller.requestData().then((value) {
                                  //   appModel.is_focus = true;
                                  //   print('value ========= ${value}');
                                  //
                                  // });
                                }
                              }),
                            )
                          ],
                        ),
                        Container(
                          height: 0.5,
                          color: AppColor.lineColor,
                        )
                      ],
                    ),
                  );
                },
                itemCount: controller.model?.data?[index].apps?.length??0,
                onReorder: (int oldIndex,int newIndex){
                  controller.listMove(oldIndex, newIndex, index);
              // setState(() {
              //   print("$oldIndex to $newIndex");
              //   if (oldIndex < newIndex) {
              //     //向下移动的情况,需要减去1，不然会index 溢出
              //     newIndex -= 1;
              //   }
              //   // final int item = itemList.removeAt(oldIndex);
              //   // itemList.insert(newIndex, item);
              //
              //   Apps reapp = _model!.data![index].apps!.removeAt(oldIndex);
              //   _model!.data![index].apps!.insert(newIndex, reapp);
              //
              //   // Car redata = datas.removeAt(oldindex);
              //   // datas.insert(newindex, redata);
              // });
            })
          ],
        );
      },
      childCount: controller.dataArr.length,
    );
  }

}